$(document).ready(function () {
    getData(p)
});

function getSortType(){
    var type = ''
    if($('#searchSelect').css('display') == 'block'){
        switch ($("#searchSelect option:selected").val()) {
            case 'beforBreak':
                type = 1
                break;
            case 'afterBreak':
                type = 2
                break;
            case 'beforLunch':
                type = 3
                break;
            case 'afterLunch':
                type = 4
                break;
        }
    }
    return type;
}

function getData(p){
    var startTime = $('#start').val() != ''? $('#start').val():'2019-01-01';//开始时间
    var endTime = $('#end').val() != ''? $('#end').val(): laydate.now(1, 'YYYY-MM-DD');//结束时间
    $.ajax({
        url: global.apiUrl + '/admin_eat_order',
        type: 'GET',
        dataType: 'json',
        data: {
            p: p,
            start_time: startTime,
            end_time: endTime,
            type: getSortType(),
            userid: $('#searchInput').val()
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(res)
            var html = ''
            for(var i in res.content){
                html += `<tr>
                            <td>
                                <div class="userItem" onclick="findId(${res.content[i].user_id})">
                                    <text>${res.content[i].user_content.nickname}</text>
                                    <text>${res.content[i].user_content.sex} ${res.content[i].user_content.height}cm</text>
                                    <text style="margin-bottom:0">ID: ${res.content[i].user_id}</text>
                                </div>
                            </td>
                            <td>
                                <img src="${global.apiUrl + getFoodImg(res.content[i].img)}"/>
                            </td>
                            <td>${getFoodType(res.content[i].type)}</td>
                            <td>${res.content[i].date}</td>
                            <td>${res.content[i].remark?res.content[i].remark:'-'}</td>
                            <td>
                                <a href="foodDetails.html?id=${res.content[i].user_id}&date=${res.content[i].date}" class="btn btn-white btn-sm">详情</a>
                            </td>
                        </tr>`
            }
            $('#userList').html(html)
            $("#page").paging({
                pageNo: p,
                totalPage: Math.ceil(res.count/20),
                totalSize: res.count,
                callback: function(num) {
                    // console.log(num)
                    getData(num,getValue(page))
                }
            })
        }
    })
}

function getFoodImg(list){
    var img =''
    if(list != ''){
        var arr = list.split(',')
        img = arr[0]
    }
    return img
}

//搜索
function search(obj)
{
    var searchInput = document.getElementById("searchInput");
    var searchSelect = document.getElementById("searchSelect");
    if(obj.value == 'ID'){
        searchInput.style.display = 'block';
        searchSelect.style.display = 'none'
    }else{
        searchInput.style.display = 'none';
        searchSelect.style.display = 'block'
        getData(p)
    }
}

function clickBtn(){
    getData(p)
}

function findId(id){
    // console.log(id)
    $("#searchSelectMain").val("ID"); 
    $("#searchInput").val(id);
    getData(p)
}